﻿#include "pipelinemanagerwid.h"
#include <QTimer>
#include "ui_pipelinemanagerwid.h"

#include "componentmanager.h"
#include "taskProcess/TaskProcess.h"
#include "pipelineinfowid.h"

PipelineManagerWid::PipelineManagerWid(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::PipelineManagerWid())
{
	ui->setupUi(this);
	ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
	ui->tableWidget->verticalHeader()->setVisible(false);
	m_task = dynamic_cast<TaskProcess*>(component->GetComponent(Component::component_taskExcutor));
	m_pipes = m_task->GetPipeList();
	int  i = 0;
	for (auto pipe : m_pipes) {
		auto temp = new PipelineInfoWid();
		temp->Init(m_task, pipe);
		temp->hide();
		m_wids.append(temp);
		ui->tableWidget->insertRow(i);
		auto item0 = new QTableWidgetItem(pipe->GetPipelineId());
		item0->setTextAlignment(Qt::AlignCenter);
		auto item1 = new QTableWidgetItem("空闲状态");
		item1->setTextAlignment(Qt::AlignCenter);
		auto item2 = new QTableWidgetItem("123");
		item2->setTextAlignment(Qt::AlignCenter);
		auto item3 = new QTableWidgetItem("箱号");
		item3->setTextAlignment(Qt::AlignCenter);

		QBrush brush(Qt::blue);
		item2->setForeground(brush); // 设置字体的颜色
		ui->tableWidget->setItem(i, 0, item0);
		ui->tableWidget->setItem(i, 1, item2);
		ui->tableWidget->setItem(i, 2, item3);
		ui->tableWidget->setItem(i, 3, item1);
		i++;
	}

	UpdatePipeTable();
}

PipelineManagerWid::~PipelineManagerWid()
{
	delete ui;
}

void PipelineManagerWid::UpdatePipeTable()
{
	int  i = 0;
	for (auto iter : m_pipes) {
		auto state = iter->GetPipelineTaskState();
		SetPipelineState(i, state);
		if (state == PipelineTaskState::pipeline_error) {
			ui->tableWidget->item(i, 0)->setBackground(QColor(Qt::red));
			ui->tableWidget->item(i, 1)->setBackground(QColor(Qt::red));
			ui->tableWidget->item(i, 2)->setBackground(QColor(Qt::red));
			ui->tableWidget->item(i, 3)->setBackground(QColor(Qt::red));
		} else {
			ui->tableWidget->item(i, 0)->setBackground(QColor(Qt::white));
			ui->tableWidget->item(i, 1)->setBackground(QColor(Qt::white));
			ui->tableWidget->item(i, 2)->setBackground(QColor(Qt::white));
			ui->tableWidget->item(i, 3)->setBackground(QColor(Qt::white));
		}

		if (state != PipelineTaskState::pipeline_task_idle) {
			auto info = iter->GetPipelineTaskInfo();
			ui->tableWidget->item(i, 1)->setText(info.taskId);
			auto sku = iter->GetPipelineTaskInfo().skuNo;
			ui->tableWidget->item(i, 2)->setText(sku);
		} else {
			ui->tableWidget->item(i, 1)->setText("");
			ui->tableWidget->item(i, 2)->setText("");
		}

		i++;
	}

	QTimer::singleShot(1000, [&]() {
		UpdatePipeTable();
		});
}

void PipelineManagerWid::SetPipelineState(int x, PipelineTaskState state)
{
	QString text;
	switch (state) {
	case PipelineTaskState::pipeline_error:
		text = "异常状态";

		break;
	case PipelineTaskState::pipeline_task_idle:
		text = "空闲状态";
		break;
	case PipelineTaskState::pipeline_task_receive:
		text = "任务接收状态";
		break;
	case PipelineTaskState::pipeline_task_inited:
		text = "任务初始化状态";
		break;
	case PipelineTaskState::pipeline_task_start:
		text = "业务线任务开始";
		break;
	case PipelineTaskState::pipeline_task_stepPrepare:
		text = "节拍准备中";
		break;
	case PipelineTaskState::pipeline_task_stepStart:
		text = "节拍准备中";
		break;
	case PipelineTaskState::pipeline_task_wait_finish:
		text = "等待路径结束";
		break;
	case PipelineTaskState::pipeline_task_finish:
		text = "业务线任务完成";
		break;
	default:
		text = "业务线状态异常";
		break;
	}
	ui->tableWidget->item(x, 3)->setText(text);
}

void PipelineManagerWid::on_tableWidget_cellPressed(int row, int colum)
{
	if (colum != 1) { // 指向taskID
		return;
	}

	auto item = ui->tableWidget->item(row, colum);
	auto task = item->text();
	if (task.isEmpty()) {
		return;
	}

	if (row < m_wids.size()) {
		m_wids.at(row)->ShowTaskInfo();
	}
}
